/**
 * 
 */
package net.mikoo.seals.boss.web.controller;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import net.mikoo.seals.boss.biz.services.MenuService;
import net.mikoo.seals.boss.biz.services.RolePermitService;
import net.mikoo.seals.boss.biz.services.RoleServices;
import net.mikoo.seals.boss.common.BaseController;
import net.mikoo.seals.boss.common.exception.ParametersException;
import net.mikoo.seals.boss.model.Menu;
import net.mikoo.seals.boss.model.MenuQuery;
import net.mikoo.seals.boss.model.Role;

/**
 * @author huangxm
 *
 */
@Controller
@RequestMapping("/admin/role")
public class RoleController extends BaseController {
	
	private Logger logger =  LoggerFactory.getLogger(RoleController.class);

	@Autowired
	private RoleServices roleServices;
	
	@Autowired
	private MenuService  menuService;
	
	@Autowired
	private RolePermitService rolePermitService;

	@RequestMapping("/list")
	public String list(HttpServletRequest request, Model model) {
		List<Role> roles = roleServices.selectAllEnabled();
		model.addAttribute("roles", roles);

		return resultView(ADMIN_VIEW, "role/list");
	}

	@RequestMapping("/form")
	public String from(Model model) {
		
		MenuQuery query =new MenuQuery();
		query.setIsEnable("Y");
		List<Menu> menus = menuService.getListByQuery(query);
		model.addAttribute("menus", menus);
		return resultView(ADMIN_VIEW, "role/form");
	}

	@RequestMapping("/create")
	@ResponseBody
	public Map<String,Object> create(HttpServletRequest request, Role role, Model model) {
		
		try {
			
			role.setEnabled("Y");
			roleServices.save(role);
			return result(SUCCESS, MSG_SUCCESS);
		}catch (ParametersException e) {
			logger.error("",e);
			return result(ERROR, e.getMessage());
		}catch (Exception e) {
			logger.error("",e);
			return result(ERROR, MSG_SYSTEM_ERROR);
		}
		
	}

	@RequestMapping("/edit")
	public String edit(Long roleId, Model model) {
		
		Role role = roleServices.loadByPrimaryKey(roleId);
		List<Menu> menus = menuService.loadAsTree();
		List<Menu> haveMenus = rolePermitService.loadResourceByRoleId(roleId);	
		Map<String,Object> mapHaveMenus = new HashMap<>();
		for (Menu menu : haveMenus) {
			mapHaveMenus.put("menu"+menu.getMenuId(), menu);
		}
		
		
		model.addAttribute("menus", menus);
		model.addAttribute("haveMenus", mapHaveMenus);
		model.addAttribute("role", role);
		return resultView(ADMIN_VIEW, "role/edit");
	}

	@RequestMapping("/modify")
	@ResponseBody
	public  Map<String,Object> modify(Role role, Model model) {
		
		try {
			roleServices.modifyByPrimaryKey(role);
			return result(SUCCESS, MSG_SUCCESS);
		}catch (ParametersException e) {
			logger.error("",e);
			return result(ERROR, e.getMessage());
		}catch (Exception e) {
			logger.error("",e);
			return result(ERROR, MSG_SYSTEM_ERROR);
		}
		
	}

	@RequestMapping("/delete")
	@ResponseBody
	public Map<String,Object> modify(Long roleId, Model model) {
		
		try {
			roleServices.deleteByPrimaryKey(roleId);
			return result(SUCCESS, MSG_SUCCESS);
		}catch (ParametersException e) {
			logger.error("",e);
			return result(ERROR, e.getMessage());
		}catch (Exception e) {
			logger.error("",e);
			return result(ERROR, MSG_SYSTEM_ERROR);
		}
	}

}
